// 请将 AppId 改为你自己的 AppId，否则无法本地测试
var appId = 'jrL2k7Ch5Xq1sYvAfuszevK2-gzGzoHsz';
var appKey = 'R3P55vWR0JayYC2BYIizfVLN';
var room = null;
AV.initialize (appId, appKey);

var roomId = 'xxx';
var room = null;
// 创建实时通信实例
var realtime = new AV.Realtime ({
  appId: appId,
  appKey: appKey,
  // plugins: TypedMessagesPlugin,
});

$ ('#login').click (() => {
  var user1 = $ ('#username_input').val ();
  var user2 = $ ('#to_user_inpout').val ();
  init (user1, user2).catch (function (err) {
    console.error (err);
  });
});

$ ('#sendbtn').click (() => {
  var value = $ ('#text').val ();
  room.send (new AV.TextMessage (value));
});
async function init (user1Id, user2Id) {
  var user1 = await realtime.createIMClient (user1Id);
  var user2 = await realtime.createIMClient (user2Id);

  room = await user1.createConversation ({
    unique: true,
    name: 'LeanCloud-Conversation',
    members: [user1Id, user2Id],
  });

  /* 
    .then (conversation => {
      return conversation.send (new AV.TextMessage ('耗子，起床！'));
    }); */

  user2.on (AV.Event.MESSAGE, function (message, conversation) {
    $ ('#lists').append (
      `<div> <strong>${message.from}:</strong><span>${message.text}</span>   </div>`
    );
  });
}
